package com.moyun.zbmy.zizhou.ui.adapter.holder;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.beans.AccountBean;
import com.moyun.zbmy.zizhou.ui.adapter.AccountAdapter;
import com.moyun.zbmy.zizhou.ui.utils.GlideCircleTransform;
import com.moyun.zbmy.zizhou.ui.utils.GlideUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 公众号列表
 */
public class AccountHolder extends BaseAbsHolder<AccountBean> implements View.OnClickListener {

    @BindView(R.id.account_img)
    protected ImageView account_img;
    @BindView(R.id.account_name)
    protected CustomFontTextView account_name;
    @BindView(R.id.account_subscribe)
    protected ImageView account_subscribe;


    private AccountBean temp;
    private GlideCircleTransform circleTransform;
    private AccountAdapter.OnAccountClickListener accountClickListener;
    private AccountAdapter.OnAccountSubscribeClickListener subscribeClickListener;

    public AccountHolder(Context context, View itemView,AccountAdapter.OnAccountClickListener accountClickListener,
              AccountAdapter.OnAccountSubscribeClickListener subscribeClickListener) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.accountClickListener = accountClickListener;
        this.subscribeClickListener = subscribeClickListener;
    }


    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AccountBean t) {
        temp = t;
        circleTransform = new GlideCircleTransform(context);
        account_name.setText(t.getFollowedName());
        if (t.getFollowedStatus()==1) {
            account_subscribe.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_subscribed));
        } else {
            account_subscribe.setImageDrawable(context.getResources().getDrawable(R.mipmap.icon_subscribe));
        }
        account_subscribe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != subscribeClickListener) {
                    subscribeClickListener.OnSubscribeClick(temp);
                }
            }
        });

        GlideUtil.getCircleGlid(context, t.getFollowedSmallImage(), true).diskCacheStrategy(DiskCacheStrategy.NONE).transform(circleTransform).into(account_img);
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);

    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        if (null != accountClickListener) {
            accountClickListener.OnAccountClick(temp);
        }
    }

}
